package cn.wawi.controller.business;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.wawi.controller.BaseController;
import cn.wawi.model.business.Serviceitem;
import cn.wawi.utils.DbUtil;

import com.jfinal.ext.route.ControllerBind;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.render.JsonRender;
import com.jfinal.upload.UploadFile;

@ControllerBind(controllerKey="/serviceitem")
public class ServiceItemController extends BaseController<Serviceitem>{
	
	/*
	 * 获取菜单树
	 */
	public void getCategory(){
		render(new JsonRender(DbUtil.findTree(Db.find("select * from servicecategory where deleted !=1 "))).forIE());
	}
	

	
	public void findAllx(){
	  List<Map<String,Object>> ltx = new ArrayList<Map<String,Object>>();
	  List<Record> lt =	Db.find("select id,name as text from serviceitem where deleted !=1 ");
	  for( Record rc  : lt )	
	  {
		  Map<String, Object> map = new HashMap<String, Object>();
		  map.put("id", rc.getInt("id"));
		  map.put("text", rc.getStr("text"));
		  ltx.add(map);
	  }
	  
	  
		render(new JsonRender(ltx).forIE());
	}
	
	public void addOne(){
		UploadFile file =  getFile();
		long fn = System.currentTimeMillis();
		file.getFile().renameTo(new File(file.getUploadPath()+"/"+fn+".jpg"));
		Model<?> m=getModel(Serviceitem.class);
        m.set("logo", "/upload/"+fn+".jpg");       
		StringBuffer sb = new StringBuffer(Arrays.toString(getRequest().getParameterValues("payway")));
		sb.deleteCharAt(0);
		sb.deleteCharAt(sb.length()-1);
		m.set("payway", sb.toString());
		if(!m.save()){
			json.setResMsg("添加失败!");
			json.setResCode(0);
		}
		render(new JsonRender(json).forIE());
	}
	
	
	/**
	 * 通用修改
	 */
	
	public void updateOne(){
		UploadFile file =  getFile();
		long fn = System.currentTimeMillis();
		file.getFile().renameTo(new File(file.getUploadPath()+"/"+fn+".jpg"));
		Model<?> m=getModel(Serviceitem.class);
        m.set("logo", "/upload/"+fn+".jpg");       
		StringBuffer sb = new StringBuffer(Arrays.toString(getRequest().getParameterValues("payway")));
		sb.deleteCharAt(0);
		sb.deleteCharAt(sb.length()-1);
		m.set("payway", sb.toString());
		if(!m.update()){
			json.setResMsg("修改失败!");
			json.setResCode(0);
		}
		render(new JsonRender(json).forIE());
	}
	
	

}
